﻿
Partial Class WebForm_ManageOrganization_AddOrganization
    Inherits BaseUserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

        End If
    End Sub

    Protected Sub btnAdd_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(sender)
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound

        If Me.fv.CurrentMode = FormViewMode.Insert Then
        End If
    End Sub


    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand
        If e.CommandName = "btnSave" Then
            If Me.DataAccessMode = DataAccessMode.Insert Then
                If TryCast(Me.fv.FindControl("txtOrganizationName"), TextBox).Text = "" Then
                    ShowMessageForAjax(fv, Me.GetLocalResourceObject("OrganizationNameEmpty"))
                    Return
                End If
                Me.AddData()
                RaiseEvent OrganizationAdded(Me, New EventArgs)
            ElseIf Me.DataAccessMode = DataAccessMode.Update Then
                Me.UpdateData()
                RaiseEvent OrganizationUpdated(Me, New EventArgs)
            End If

            RaiseEvent Saved(Me, New EventArgs)

        End If
    End Sub


End Class

Partial Class WebForm_ManageOrganization_AddOrganization
    Private Function AddData() As Boolean
       
        Dim bbl As WorkFlow.BBL.OrgBBL = New WorkFlow.BBL.OrgBBL()
    
        Me.OrganizationID = bbl.InsertOrg(TryCast(Me.fv.FindControl("txtOrganizationName"), TextBox).Text, Me.ParentID, TryCast(Me.fv.FindControl("ddlActive"), DropDownList).SelectedValue, TryCast(Me.fv.FindControl("txtOrgEngName"), TextBox).Text, TryCast(Me.fv.FindControl("txtOrgChnName"), TextBox).Text, dbName)

        Me.organizationNameField = TryCast(Me.fv.FindControl("txtOrganizationName"), TextBox).Text

        clsAuth.BuildOrganizationCode(dbName)

   
        Return True
    End Function

    Private Function UpdateData() As Boolean

        Dim bbl As WorkFlow.BBL.OrgBBL = New WorkFlow.BBL.OrgBBL()
        bbl.UpdateOrg(TryCast(Me.fv.FindControl("txtOrganizationName"), TextBox).Text, TryCast(Me.fv.FindControl("ddlActive"), DropDownList).SelectedValue, TryCast(Me.fv.FindControl("txtOrgEngName"), TextBox).Text, TryCast(Me.fv.FindControl("txtOrgChnName"), TextBox).Text, Me.OrganizationID, dbName)

        Me.organizationNameField = TryCast(Me.fv.FindControl("txtOrganizationName"), TextBox).Text

        clsAuth.BuildOrganizationCode(dbName)

      
        Return True
    End Function



    ''' <summary>
    ''' 新增模式
    ''' </summary>
    ''' <param name="parentID"></param>
    ''' <remarks></remarks>
    Public Sub Show(ByVal parentID As String)
        Me.DataAccessMode = My.DataAccessMode.Insert
        Me.fv.ChangeMode(FormViewMode.Insert)
        Me.ParentID = parentID


        Me.Visible = True
    End Sub

    ''' <summary>
    ''' 編輯模式
    ''' </summary>
    ''' <param name="organizationID"></param>
    ''' <remarks></remarks>
    Public Sub ShowEdit(ByVal organizationID As String)
        Me.DataAccessMode = My.DataAccessMode.Update
        Me.fv.ChangeMode(FormViewMode.Edit)
        Me.OrganizationID = organizationID

        Me.BindDataForEdit()


        Me.Visible = True
    End Sub

    Private Sub BindDataForEdit()
        Dim bbl As WorkFlow.BBL.OrgBBL = New WorkFlow.BBL.OrgBBL()
        Dim dt As Data.DataTable = bbl.GetOrg(Me.OrganizationID, dbName)
        Me.fv.DataSource = dt
        Me.fv.DataBind()


        TryCast(Me.fv.FindControl("ddlActive"), DropDownList).SelectedValue = dt.Rows(0)("Active")

    End Sub

    Private organizationNameField As String

    Public ReadOnly Property OrganizationName() As String
        Get
            Return Me.organizationNameField
        End Get
    End Property

    Private Property ParentID() As String
        Get
            Return ViewState("ParentID")
        End Get
        Set(ByVal value As String)
            ViewState("ParentID") = value
        End Set
    End Property

    Public Property OrganizationID() As Integer
        Get
            Return ViewState("OrganizationID")
        End Get
        Set(ByVal value As Integer)
            ViewState("OrganizationID") = value
        End Set
    End Property

    Public Event OrganizationAdded(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event OrganizationUpdated(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event Saved(ByVal sender As Object, ByVal e As System.EventArgs)

    Private Property DataAccessMode() As My.DataAccessMode
        Get
            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As My.DataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property
End Class